/*
*	ID : Pallab
* 	PROG : C
* 	LANG : C++
* 	2012-11-29-14.06.23 Thursday
*
* 	"I have not failed, I have just found 10000 ways that won't work.
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <numeric>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>
#include <unordered_map>
#include <unordered_set>
#include <tuple>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define foR(i1,st,ed) for(int i1 = st , j1 = ed ; i1 < j1 ; ++i1 )
#define fo(i1,ed) foR( i1 , 0 , ed )
#define foE(i1,st,ed) foR( i1, st, ed+1 )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define line puts("")

inline void wait(double seconds) {
    double endtime = clock() + (seconds * CLOCKS_PER_SEC);
    while (clock() < endtime) {
        ;
    }
}
inline Int encodePos(Int rpos, Int cpos, Int RR, Int CC) {
    return rpos*CC + cpos ;
}
inline Int decodeR(Int rpos, Int RR, Int CC) {
    return rpos/CC;
}
inline Int decodeC(Int cpos, Int RR, Int CC) {
    return cpos%CC;
}
template<class T>
inline T fastIn() {
    register char c=0;
    register T a=0;
    bool neg=false;
    while(c<33)c=getchar();
    while(c>33) {
        if(c=='-') {
            neg=true;
        } else {
            a= (a*10)+(c-'0');
        }
        c=getchar();
    }
    return neg?-a:a;
}
const Int INF = (Int)1e9+1;
const int MX = (int)1e5+5;
Int sr,sc,dr,dc;

unordered_set<Int> memo;
inline void read() {
    sr=fastIn<Int>(),sc=fastIn<Int>(),dr=fastIn<Int>(),dc=fastIn<Int>();
    int segment_counts=fastIn<int>();
    fo(i,segment_counts) {
        Int R=fastIn<Int>();
        Int from=fastIn<Int>();
        Int to=fastIn<Int>();
        while( from <=to ) {
            memo.insert( encodePos(R,from,INF,INF) );
            ++from;
        }
    }
}
struct node {
    Int pos;
    Int M;
};
const int rr[]= {-1,+1,+0,+0, -1,-1,+1,+1};
const int cc[]= {+0,+0,-1,+1, -1,+1,+1,-1};
inline void proc() {
    unordered_set<Int> visited;
    Int src = encodePos(sr,sc,INF,INF);
    Int snk = encodePos(dr,dc,INF,INF);
    queue<node> q;
    q.push( node {src,0} );
    visited.insert(src);

    while(q.empty()==false) {
        node f=q.front();
        q.pop();

        if(f.pos==snk) {
            cout<<f.M<<'\n';
            return ;
        }

        for(int i=0; i<8; ++i) {
            Int tr=decodeR(f.pos,INF,INF)+rr[i];
            Int tc=decodeC(f.pos,INF,INF)+cc[i];
            if(tr<0||tc<0||tr>INF||tc>INF)continue;
            Int nxt_pos=encodePos(tr,tc,INF,INF);
            if( memo.count(nxt_pos)>0 ) {
                if( visited.count(nxt_pos)<1 ) {
                    visited.insert(nxt_pos);
                    q.push( node {nxt_pos,f.M+1} );
                }

            }
        }
    }
    puts("-1");
}
int main() {
    int kase = 1;
#if defined( xerxes_pc )
    if (!freopen("in1", "r", stdin))
        puts("error opening file in "), assert(0);
    kase = 1;
#endif
    fo(i,kase) {
        read();
        proc();
    }
    return 0;
}
